package cn.bigcontainer.alt.sys.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.bigcontainer.alt.common.util.WoKit;
import cn.bigcontainer.alt.sys.dto.MenuDto;
import cn.bigcontainer.alt.sys.dto.RoleDto;
import cn.bigcontainer.alt.sys.dto.UserDto;
import cn.bigcontainer.alt.sys.exception.SysException;
import cn.bigcontainer.alt.sys.mapper.MenuMapper;
import cn.bigcontainer.alt.sys.mapper.RoleMapper;
import cn.bigcontainer.alt.sys.mapper.UserMapper;
import cn.bigcontainer.alt.sys.po.Menu;
import cn.bigcontainer.alt.sys.po.Role;
import cn.bigcontainer.alt.sys.po.User;
import cn.bigcontainer.alt.sys.service.MainService;

@Service
@Transactional
public class MainServiceImpl implements MainService {

	@Resource // @Autowired
	private MenuMapper menuMapper;

	@Resource // @Autowired
	private UserMapper userMapper;

	@Resource // @Autowired
	private RoleMapper roleMapper;
	
	@Override
	public UserDto authenticate(String username, String password) {
		// 1.认证
		// 获取用户
		User u = userMapper.selectOne(new QueryWrapper<User>() {{
			eq("login_name", username);
		}});
		// 认证
		if (u == null) {
			throw new SysException(SysException.ERR_LOGIN);
		}
		if (!u.getPassword().equals(WoKit.getMD5(u.getId().toString(), password))) {
			throw new SysException(SysException.ERR_LOGIN);
		}
		// 2.获取用户有权限的菜单集合，并按照no排序
		List<Integer> menuIdsByUser = menuMapper.selectMenuIdsByUser(u.getId());
		if (menuIdsByUser.size() == 0) {
			throw new SysException(SysException.ERR_PERMISSION);
		}
		List<Menu> menus = menuMapper.selectList(new QueryWrapper<Menu>().in("id", menuIdsByUser));
		Collections.sort(menus, new Comparator<Menu>() {
			@Override
			public int compare(Menu o1, Menu o2) {
				return o1.getNo().compareTo(o2.getNo());
			}
		});
		List<MenuDto> menuDtos = WoKit.getDtos(MenuDto.class, menus);
		// List<MenuDto> menuDtos = WoKit.getDtos(MenuDto.class, menuMapper.selectMenuIdsByUser(u.getId()));
		List<MenuDto> tops = new ArrayList<>();
		for (MenuDto m : menuDtos) {
			if (m.getParentId() == null) {
				tops.add(m);
				m.getChildren().clear();
			} else {
				tops.get(tops.size() - 1).getChildren().add(m);
			}
		}
		// 3.构造UserDto并设置其有权限的顶级菜单
		UserDto dto = new UserDto(u);
		dto.setTops(tops);
		// 4.设置角色
		List<Role> roles = roleMapper.selectList(new QueryWrapper<Role>().inSql("id", "select role_id from sys_user_role where user_id = " + dto.getId()));
		dto.setRoles(WoKit.getDtos(RoleDto.class, roles));
		return dto;
	}

	@Override
	public void modifyPassword(String oldPassword, String newPassword, String newPassword2, UserDto currentUserWithException) {
		if (WoKit.isEmpty(oldPassword)) {
			throw new SysException(SysException.ERROR_NULL, "初始密码");
		}
		if (WoKit.isEmpty(newPassword)) {
			throw new SysException(SysException.ERROR_NULL, "新密码");
		}
		if (WoKit.isEmpty(newPassword2)) {
			throw new SysException(SysException.ERROR_NULL, "再次输入新密码");
		}
		if (!newPassword.equals(newPassword2)) {
			throw new SysException(SysException.ERROR_NOT_SAME, "新密码", "再次输入新密码");
		}
		User user = userMapper.selectById(currentUserWithException.getId());
		if (!WoKit.getMD5(user.getId(), oldPassword).equals(user.getPassword())) {
			throw new SysException(SysException.ERROR_NOT_SAME, "初始密码", "原密码");
		}
		user.setPassword(WoKit.getMD5(user.getId(), newPassword));
		userMapper.updateById(user);
	}

}
